package com.ktg.mes.erpBom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.mes.erpBom.service.IErpProductStructureService;
import com.ktg.mes.erpBom.service.IT6ErpProductStructureItemService;
import com.ktg.mes.erpBom.service.IT6ErpProductStructureService;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.domain.MdProductBom;
import com.ktg.mes.md.service.IMdProductBomService;
import com.ktg.mes.md.service.impl.MdProductSopServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: T6 ERP系统-产品结构档案主表
 * @Author: jeecg-boot
 * @Date:   2022-08-20
 * @Version: V1.0
 */
@Service
public class ErpProductStructureServiceImpl implements IErpProductStructureService {

    @Autowired
    private IT6ErpProductStructureItemService it6ErpProductStructureItemService;

    @Autowired
    private IMdProductBomService productBomService;


    /**
     * 处理同步业务
     */
    @Override
    public void syncDataListByLastIdFromErp() {
        String lastId = "0";
        // 处理 MesInventory 数据
        String maxPubfts = productBomService.queryMaxPubfts();
        if (StringUtils.isNotBlank(maxPubfts)) {
            lastId = maxPubfts;
        }
        List<MdProductBom> list = it6ErpProductStructureItemService.queryListByLastPubufts(lastId);
        for (MdProductBom item : list) {
            MdProductBom findItem = new MdProductBom();
            findItem.setBomItemCode(item.getBomItemCode());
            findItem.setAutoId(item.getAutoId());
            List<MdProductBom> list1 = productBomService.selectMdProductBomList(findItem);
            if (list1.size() > 0) {
                // 不更新 inv_cat_code
                MdProductBom oldItem = list1.get(0);
                item.setBomId(oldItem.getBomId());
                productBomService.updateMdProductBom(item);
            }else {
                productBomService.insertMdProductBom(item);
            }
        }
    }
}
